package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.engineering.customview.AddProductInforPoupWindow;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tony on 2017/12/2.
 */

public class AddProductInfomationActivity extends Activity implements View.OnClickListener ,ItemClickListener {
    private View rl_back;
    private TextView tv_title;
    private RelativeLayout rl_rect_content, rl_circular_content, rl_drawings_content;
    private RadioGroup rg_associated_size;
    private RadioButton rb_rect;
    private Button btn_upload;
    private EditText edt__product_name, edt__product_quatity, edt__rect_length, edt__rect_high, edt__rect_thickness;
    // edt__rect_left_side_up, edt__rect_left_side_down, edt__rect_left_side_left, edt__rect_left_side_right,
    private EditText edt__drawings_specific, edt__hateromorphism_instruction, edt__circular_diametra, edt__circular_thickness;
    //edt__circular_left_side
    private RelativeLayout rl_unit;
    private View view_unit;
    private TextView edt__unit;
    private ImageView img_unit;
    private String id = "";
    private String name = "";
    private String unit = "套";
    private List<String> picUrl;
    private int diameter, height, thick, width, amount;
    //border
    // borderBottom, borderLeft, borderRight, borderTop
    private String desc = "", type = "RECTANGLE";

    private PopupWindow mPopupWindow;

    private AddProductInforPoupWindow addProductInforPoupWindow;
    private String datas[] = {"套", "只", "个","箱","件","台","条"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_add_product_infomation2);

        init();


    }

    private void init() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_rect_content = findViewById(R.id.rl_rect_content);
        rl_circular_content = findViewById(R.id.rl_circular_content);
        rl_drawings_content = findViewById(R.id.rl_drawings_content);
        rg_associated_size = findViewById(R.id.rg_associated_size);
        rb_rect = findViewById(R.id.rb_rect);
        btn_upload = findViewById(R.id.btn_upload);
        edt__product_name = findViewById(R.id.edt__product_name);
        edt__product_quatity = findViewById(R.id.edt__product_quatity);
        view_unit=findViewById(R.id.view_unit);
        edt__unit = findViewById(R.id.edt__unit);
        img_unit=findViewById(R.id.img_unit);
        rl_unit=findViewById(R.id.rl_unit);
        edt__rect_length = findViewById(R.id.edt__rect_length);
        edt__rect_high = findViewById(R.id.edt__rect_hight);
        edt__rect_thickness = findViewById(R.id.edt__rect_thickness);
//        edt__rect_left_side_up = findViewById(R.id.edt__rect_left_side_up);
//        edt__rect_left_side_down = findViewById(R.id.edt__rect_left_side_down);
//        edt__rect_left_side_left = findViewById(R.id.edt__rect_left_side_left);
//        edt__rect_left_side_right = findViewById(R.id.edt__rect_left_side_right);
        edt__circular_diametra = findViewById(R.id.edt__circular_diametra);
//        edt__circular_left_side = findViewById(R.id.edt__circular_left_side);
        edt__circular_thickness = findViewById(R.id.edt__circular_thickness);

        edt__drawings_specific = findViewById(R.id.edt__drawings_specific);
        edt__hateromorphism_instruction = findViewById(R.id.edt__hateromorphism_instruction);

//        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
//            @Override
//            public void onItemSelected(AdapterView<?> parent, View view,
//                                       int pos, long id) {
//
//                String[] languages = getResources().getStringArray(R.array.languages);
//                Toast.makeText(MainActivity.this, "你点击的是:"+languages[pos], 2000).show();
//            }
//            @Override
//            public void onNothingSelected(AdapterView<?> parent) {
//                // Another interface callback
//            }
//        });
//    }


        tv_title.setText(getResources().getText(R.string.add_product_infomation));
        rl_back.setOnClickListener(this);
        btn_upload.setOnClickListener(this);
        img_unit.setOnClickListener(this);
        rb_rect.setChecked(true);
        rg_associated_size.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) {
                if (id == R.id.rb_rect) {
                    type = "RECTANGLE";
                    showShapContent();
                } else if (id == R.id.rb_circular) {
                    type = "ROUND";
                    showShapContent();
                } else if (id == R.id.rb_hateromorphism) {
                    type = "SPECIALSHAPE";
                    showShapContent();
                }


            }
        });

        picUrl = new ArrayList<>();
        picUrl.add("");

    }

    /**
     * 根据选中的形状，显示不同的内容
     */

    private void showShapContent() {
        if (type.equals("RECTANGLE")) {
            rl_rect_content.setVisibility(View.VISIBLE);
            rl_circular_content.setVisibility(View.GONE);
            rl_drawings_content.setVisibility(View.GONE);
        } else if (type.equals("ROUND")) {
            rl_rect_content.setVisibility(View.GONE);
            rl_circular_content.setVisibility(View.VISIBLE);
            rl_drawings_content.setVisibility(View.GONE);

        } else if (type.equals("SPECIALSHAPE")) {
            rl_rect_content.setVisibility(View.GONE);
            rl_circular_content.setVisibility(View.GONE);
            rl_drawings_content.setVisibility(View.VISIBLE);

        }

    }

    /**
     * 上传产品信息
     */
    private void upload() {
        name = edt__product_name.getText().toString().trim();
        String quality = edt__product_quatity.getText().toString().trim();
        if (!TextUtils.isEmpty(quality)) {
            amount = Integer.parseInt(quality);
        }
        unit = edt__unit.getText().toString().trim();


        ProductBean productBean = new ProductBean();
        productBean.setAmount(amount);
        productBean.setName(name);
        productBean.setPicUrl(picUrl);
        productBean.setUnit(unit);
        if (type.equals("RECTANGLE")) {
//            String top = edt__rect_left_side_up.getText().toString().trim();
//            if (!TextUtils.isEmpty(top)) {
//                borderTop = Integer.parseInt(top);
//            }
//            String bottom = edt__rect_left_side_down.getText().toString().trim();
//            if (!TextUtils.isEmpty(bottom)) {
//                borderBottom = Integer.parseInt(bottom);
//            }
//            String left = edt__rect_left_side_left.getText().toString().trim();
//            if (!TextUtils.isEmpty(left)) {
//                borderLeft = Integer.parseInt(left);
//            }
//            String right = edt__rect_left_side_right.getText().toString().trim();
//            if (!TextUtils.isEmpty(right)) {
//                borderRight = Integer.parseInt(right);
//            }

            String lenght = edt__rect_length.getText().toString().trim();
            if (!TextUtils.isEmpty(lenght)) {
                width = Integer.parseInt(lenght);
            }

            String heigh = edt__rect_high.getText().toString().trim();
            if (!TextUtils.isEmpty(heigh)) {
                height = Integer.parseInt(heigh);
            }

            String thck = edt__rect_thickness.getText().toString().trim();
            if (!TextUtils.isEmpty(thck)) {
                thick = Integer.parseInt(thck);
            }


//            productBean.setBorderTop(borderTop);
//            productBean.setBorderBottom(borderBottom);
//            productBean.setBorderRight(borderRight);
//            productBean.setBorderLeft(borderLeft);
            productBean.setWidth(width);
            productBean.setHeight(height);
            productBean.setThick(thick);
            productBean.setType(CommonEnum.ProductShapeType.RECTANGLE);
        } else if (type.equals("ROUND")) {
            String diamete = edt__circular_diametra.getText().toString().trim();
            if (!TextUtils.isEmpty(diamete)) {
                diameter = Integer.parseInt(diamete);
            }
            String circular_thick = edt__circular_thickness.getText().toString().trim();
            if (!TextUtils.isEmpty(circular_thick)) {
                thick = Integer.parseInt(circular_thick);
            }

//            String circular_border = edt__circular_left_side.getText().toString().trim();
//            if (!TextUtils.isEmpty(circular_border)) {
//                border = Integer.parseInt(circular_border);
//            }
            productBean.setDiameter(diameter);
            productBean.setThick(thick);
//            productBean.setBorder(border);
            productBean.setType(CommonEnum.ProductShapeType.ROUND);

        } else if (type.equals("SPECIALSHAPE")) {
            String hateromorphism_desc = edt__hateromorphism_instruction.getText().toString().trim();
            if (!TextUtils.isEmpty(hateromorphism_desc)) {
                desc = hateromorphism_desc;
            }
            productBean.setDesc(desc);
            productBean.setThick(thick);
            productBean.setType(CommonEnum.ProductShapeType.SPECIALSHAPE);
        }

        Intent mIntent = new Intent();

        mIntent.putExtra(ConstantUtil.PRODUCTBEAN, productBean);
        setResult(ConstantUtil.ADD_PRODUCT_INFOMATION, mIntent);

        Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show();
        finish();

    }

    // 单位
    private void showUnitWindow(View anchor) {
        addProductInforPoupWindow = new AddProductInforPoupWindow(this, datas);
        addProductInforPoupWindow.setItemClick(this);
        addProductInforPoupWindow.showPopupWindow(anchor);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.btn_upload:
                upload();
                break;
            case R.id.img_unit:
                showUnitWindow(view_unit);
//             View contentView = LayoutInflater.from(this).inflate(R.layout.item_add_product_infor, null);
//				TextView tao = (TextView) contentView.findViewById(R.id.tao);
//				TextView zhi = (TextView) contentView.findViewById(R.id.zhi);
//				TextView ge = (TextView) contentView.findViewById(R.id.ge);
//				TextView xiang = (TextView) contentView.findViewById(R.id.xiang);
//                TextView jian = (TextView) contentView.findViewById(R.id.jian);
//                TextView tai = (TextView) contentView.findViewById(R.id.tai);
//                TextView tiao = (TextView) contentView.findViewById(R.id.tiao);
//
//                tao.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        edt__unit.setText("套");
//                        mPopupWindow.dismiss();
//                    }
//                });
//
//                zhi.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        edt__unit.setText("只");
//                        mPopupWindow.dismiss();
//                    }
//                });
//
//                ge.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        edt__unit.setText("个");
//                        mPopupWindow.dismiss();
//                    }
//                });
//
//                xiang.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        edt__unit.setText("箱");
//                        mPopupWindow.dismiss();
//                    }
//                });
//
//                jian.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        edt__unit.setText("件");
//                        mPopupWindow.dismiss();
//                    }
//                });
//
//                tai.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        edt__unit.setText("台");
//                        mPopupWindow.dismiss();
//                    }
//                });
//
//                tiao.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        edt__unit.setText("tiao");
//                        mPopupWindow.dismiss();
//                    }
//                });
//
//               mPopupWindow = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//				mPopupWindow.setTouchable(true);
//			   /* 设置点击menu以外其他地方以及返回键退出 */
//				mPopupWindow.setFocusable(true);
//				// 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
//				mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
//				mPopupWindow.showAsDropDown(view);
                break;
        }
    }

    @Override
    public void itemClick(View view, int position) {
        edt__unit.setText(datas[position]);
    }
}
